package com.snopzyz.hello.databus.enums;

import com.github.shyiko.mysql.binlog.event.EventType;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum DmlType {

    INSERT(1),
    UPDATE(2),
    DELETE(3)
    ;

    private int code;

    public static DmlType getByCode (int code) {
        for (DmlType type : DmlType.values()) {
            if (type.code == code) {
                return type;
            }
        }
        return null;
    }

    public static DmlType getByEventType (EventType eventType) {
        if (EventType.isWrite(eventType)) {
            return INSERT;
        } else if (EventType.isUpdate(eventType)) {
            return UPDATE;
        } else if (EventType.isDelete(eventType)) {
            return DELETE;
        }
        return null;
    }
}
